var fs = require('fs');
var path = require("path");

/**
 * @param {string} absPath
 * @return {[]string}
 */
var read_file_to_string = function(absPath){
	var readret = fs.readFileSync(absPath,'utf-8');
	if (readret == null){
		return null;
	}
	else{
		var readretArray = readret.split('\n');
		var realRet = [];
		for (var i = 0; i < readretArray.length; i++){
			//考虑到可能是windows平台上保存的
			if (readretArray[i].length != 0){
				if (readretArray[i][readretArray[i].length - 1] == '\r'){
					readretArray[i] = readretArray[i].substring(0, readretArray[i].length - 1);
				}
				if (readretArray[i].length != 0) {
					realRet.push(readretArray[i]);				
				}
			}
		}
		return realRet;
	}
}

/**
 * @param {string, string, bool} absPath, data
 * @return {}
 */
var write_string_to_file_appendOrTruncated = function(absPath, data, isAppend){
	if (isAppend){
		fs.appendFileSync(absPath, data);
	}
	else{
		fs.writeFileSync(absPath, data);
	}
}

//testinclude_js.js
/**
 * @param {[]int, []int} strA, strB
 * @return {[]int}
 * note: just do something with strA and return it
 */
var mergeTwoArraysII = function(strA, strB) {
    var indexB = 0;
    for (var i = 0; i < strA.length; i++){
        if (strA[i] == 0){
            strA[i] = strB[indexB];
            indexB++;
        }
    }

	return strA;
};

//runtest_js.js

//以下为main函数部分
/**
 * @param {string[]} arguments
 * @return {int}
 *
 *

 * note: must input with exactedly three para, first is testID
 *    second must be a name with full directory, eq: testproblems/sort/, third is type: "0": one custom testcase, "1": many testcases
 * return code
 *        0x30: OK; 
 *        0x31: para error;
 *        0x32: input file not occured;
 *        0x33: input data not valid/inputSubmit.txt or outputSubmit.txt file not valid or their lines number not matched; 
 *        0x34: output file cannot saved;
 *        0x35: wrong answer
 *        0x36: debug
 *        0x01: node exception return error
 *        the other codes are not predifined, if have other normal exit with exitcode(program normal exit by inside fuction runtime error!, if occur, tell me), in common condition, YOU SHOULD NOT use these not predifined codes here, otherwise confusing question may occur!

 */
var main = function(arguments) {
	//console.log(arguments.length);
	//console.log(arguments);
	
	if (arguments.length != 3){
		return 0x31;
	}else{
		var testId = arguments[0];     //
		var directory = arguments[1];
		var choose = arguments[2];
		var tempOut = "";
		
		if (choose != "0" && choose != "1"){
			return 0x31;
		}
		
		if (choose == "0"){
			var absInputPath = path.resolve(__dirname, testId + "input.txt");
			var readret = read_file_to_string(absInputPath);
			if (readret == null){
				return 0x32;
			}
			
			//console.log(readret);
			
			var absOutputPath = path.resolve(__dirname, testId + "output.txt");
			write_string_to_file_appendOrTruncated(absOutputPath, readret[0], false);
			
			var inputObject1 = JSON.parse(readret[0]);
			var inputObject2 = JSON.parse(readret[1]);
			
			var outputObject;
			
			if ((Object.prototype.toString.call(inputObject1) === '[object Array]') &&
			   (Object.prototype.toString.call(inputObject2) === '[object Array]')) {
				outputObject = mergeTwoArraysII(inputObject1, inputObject2);
			}
			else{
				return 0x33;
			}
			
			JSONres = JSON.stringify(outputObject);
			write_string_to_file_appendOrTruncated(absOutputPath, "\ncustomtest&" + JSONres + "&no compare res:(, not implemented yet", true);
			
			return 0x30;
		}
		else{
			var absInputSubmitPath = path.resolve(__dirname, "inputSubmit.txt");
			var absOutputSubmitPath = path.resolve(__dirname, "outputSubmit.txt");
			var inputret = read_file_to_string(absInputSubmitPath);
			var outret = read_file_to_string(absOutputSubmitPath);
			var absOutputPath = path.resolve(__dirname, testId + "output.txt");
			
			if (inputret == null || outret == null || inputret.length != outret.length){
				return 0x32;
			}

			//console.log(readret);
			for (var line = 0; line < outret.length; line++){				
				write_string_to_file_appendOrTruncated(absOutputPath, inputret[2*line + 0], false);
				
				var inputObject1 = JSON.parse(inputret[2*line + 0]);
				var inputObject2 = JSON.parse(inputret[2*line + 1]);

				var outputObject;
				
				if ((Object.prototype.toString.call(inputObject1) === '[object Array]') &&
				(Object.prototype.toString.call(inputObject2) === '[object Array]')) {
					outputObject = mergeTwoArraysII(inputObject1, inputObject2);
				}
				else{
					return 0x33;
				}
				
				JSONres = JSON.stringify(outputObject);
				
				if (JSONres != outret[line]){ //WA
					write_string_to_file_appendOrTruncated(absOutputPath, 
						"\ntestcase" + line.toString() + "&" + JSONres + "&" + outret[line], 
						true);
					return 0x35;
				}
			}
			
			write_string_to_file_appendOrTruncated(absOutputPath, 
				"AC\npassed all" + outret.length.toString() + " testcases &AC&AC", 
				false);
			return 0x30;
		}
	}
};

var arguments = process.argv.splice(2);
process.exit(main(arguments));